/******************************************************************************
	This file is a part of the Snapshot Bioinformatics Project

	Copyright (C) 2012-2013 Jarom Schow

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#ifndef TAXON_UPDATE_TREE_H
#define TAXON_UPDATE_TREE_H

#include <QtCore/QString>
#include <QtCore/QHash>

class QTextStream;

namespace Bio {

class Progress;
class SqlServer;
class SqlInsertBuilder;

class TaxonUpdateNode {
public:
	TaxonUpdateNode();
	TaxonUpdateNode( int taxId, int parentId, const QString &rank, int genCodeId, int mitoGenCodeId );
	TaxonUpdateNode( int localId, int taxId, int localParentId, const QString &rank, int genCodeId, int mitoGenCodeId, int left, int right );
	virtual ~TaxonUpdateNode();

	TaxonUpdateNode*	firstChild() const;
	TaxonUpdateNode*	nextSibling() const;

	TaxonUpdateNode*	takeFirstChild();

	void	addChild( TaxonUpdateNode *child );

	int		getNumChildren( bool recursive = false );

	void	setTaxonId( int id );
	int		getTaxonId() const;

	void	setParentId( int id );
	int		getParentId() const;

	void	setGeneticCode( int id );
	int		getGeneticCode() const;

	void	setMitoGeneticCode( int id );
	int		getMitoGeneticCode() const;

	void	setLocalId( int id );
	int		getLocalId() const;

	void	setLocalParentId( int id );
	int		getLocalParentId() const;

	void	setRank( const QString &rank );
	QString	getRank() const;

	void	update( const QString &rank, int genCodeId, int mitoGenCodeId );

	void	finalize( int nextTaxonId );
	bool	updateDB( SqlServer &server, Progress *progress = NULL );
	void	print( QTextStream &os );

private:

	enum Flags {
		UpdateNone =		0x0000, 
		UpdateTaxonId =		0x0001,
		UpdateParentId =	0x0002,
		UpdateGenCode =		0x0004,
		UpdateMitoGenCode =	0x0008,
		UpdateRank =		0x0010,
		UpdateLeft =		0x0020,
		UpdateRight =		0x0040,
		UpdateAll =			0x007F,
		UpdateInsert =		0x0100
	};

	void	finalizeRecurse( int &nextTaxonId, int &rightLeft );
	bool	updateDBRecurse( SqlServer &server, SqlInsertBuilder &insert, Progress *progress );
	bool	updateDBInternal( SqlServer &server );
	bool	insertInDB( SqlInsertBuilder &insert );
	void	printRecurse( QTextStream &os, int level );

	int		m_taxId, m_parentId, m_genCodeId, m_mitoGenCodeId;
	int		m_localId, m_localParentId, m_left, m_right, m_flags;
	QString	m_rank;

	TaxonUpdateNode	*m_firstChild, *m_lastChild, *m_nextSibling;

	static int	s_taxaInserted, s_taxaUpdated;
};

class TaxonUpdateName {
public:
	TaxonUpdateName();

	void	addName( const QString &name, const QString &nameClass );

	int		getNumNames() const;
	QString	getName( int which ) const;
	QString	getNameClass( int which ) const;

	QListIterator< QPair<QString,QString> >	getNameIterator() const;

private:
	QList< QPair<QString,QString> >	m_names;
};

class TaxonUpdateTree {
public:
	TaxonUpdateTree();
	virtual ~TaxonUpdateTree();

	bool	insertNode( int taxId, int parentId, const QString &rank, int genCodeId, int mitoGenCodeId );
	bool	insertName( int taxId, const QString &name, const QString &nameClass );

	bool	finalize( SqlServer &server );

	bool	loadFromDB( SqlServer &server );
	bool	updateDB( SqlServer &server );
	void	print( QTextStream &os );

private:
	TaxonUpdateNode				*m_root, *m_orphans;
	QHash<int,TaxonUpdateNode*>	m_lookupTable;
	QHash<int,TaxonUpdateName>	m_names;
};

}; // Bio

#endif // TAXON_UPDATE_TREE_H